package com.skj.server;

import javax.jdo.* ;
import com.google.appengine.api.datastore.* ;

public class AdminResDatabase
{
	private final PersistenceManagerFactory pmfInstance ;

    public AdminResDatabase()
    {
    	pmfInstance = JDOHelper.getPersistenceManagerFactory("transactions-optional");
    }
    
    public void createTextData(String location, String mimeType, Blob content)
    {
    	Resource resource = new Resource(location, mimeType, content) ;
    	
    	PersistenceManager pm = pmfInstance.getPersistenceManager();
    	
    	try
    	{
            pm.makePersistent(resource) ;
        }
    	finally
    	{
            pm.close();
        }
    }
    
    public Resource retrieveTextData(String location)
    {
    	PersistenceManager pm = pmfInstance.getPersistenceManager();
    	Resource resource ;
    	
    	try
    	{
    		resource = pm.getObjectById(Resource.class, location);
        }
    	catch(Exception e)
    	{
    		resource = null ;
    	}
    	finally
    	{
            pm.close();
        }
    	
    	return resource ;
    }
    
    public void updateTextData(String location, Blob content)
    {
    	PersistenceManager pm = pmfInstance.getPersistenceManager();
    	
    	try
    	{
    		Resource resource = pm.getObjectById(Resource.class, location);
    		resource.setContent(content) ;
    		pm.makePersistent(resource) ;
        }
    	finally
    	{
            pm.close();
        }
    }
    
    public void deleteTextData(String location)
    {
    	PersistenceManager pm = pmfInstance.getPersistenceManager();
    	
    	try
    	{
    		Resource resource = pm.getObjectById(Resource.class, location);
    		pm.deletePersistent(resource);
        }
    	finally
    	{
            pm.close();
        }
    }
}
